package com.netease.nie.service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.netease.nie.base.utils.CustomDateFormatUtils;
import com.netease.nie.constants.CustomGoodsConstants;
import com.netease.nie.dao.OrdersDao;
import com.netease.nie.model.Orders;

/**
 * 订单
 * @author wwbn3695
 * @createDate July 16th,2016
 *
 */
@Service
public class OrderServiceImpl implements OrderService {

	@Autowired
	private OrdersDao orderDao;
	
	public List<Orders> listLastThreeMonthsOrderByUserId(long userId, int pageIndex, int pageSize) {
		long now = System.currentTimeMillis();
		long threeMonthAgo = DateUtils.addMonths(new Date(now), -3).getTime();
		
		int currentTime = CustomDateFormatUtils.getyyyyMMddInt(now);
		int threeMonthAgoInt = CustomDateFormatUtils.getyyyyMMddInt(threeMonthAgo);
		
		List<Orders> orderList = 
				orderDao.listLastThreeMonthsOrderByUserId(userId, pageIndex, pageSize, currentTime, threeMonthAgoInt);
		
		if (orderList == null)
			return Collections.emptyList();
		return orderList;
	}
	
	public Orders getOrderByUserIdGoodsId(long userId, int goodsId, int exchangeCodeId) {
		Orders order = orderDao.getOrderByUserIdGoodsId(userId, goodsId, exchangeCodeId);
		return order;
	}
	
	public Orders getOrderByOrderIdAndUserId(long orderId, long userId) {
		Orders orders = orderDao.getOrderByOrderIdAndUserId(orderId, userId);
		return orders;
	}
	
	public Long insertOrder(Orders orders) {
		try {
			return orderDao.insertOrder(orders);
		} catch (Exception e) {
			return 0L;
		}
	}

	public List<Orders> queryOrdersByUserIdAndCustomGoodsCode(long userId,
			CustomGoodsConstants customGoodsConstants, int pageIndex, int pageSize) {
		List<Orders> orderList = orderDao.queryOrdersByUserIdAndCustomGoodsCode(userId, customGoodsConstants, pageIndex, pageSize);
		if (orderList != null && !orderList.isEmpty())
			return orderList;
		return Collections.emptyList();
	}

}
